package object2;

import java.lang.Override;

/**
 * 多态参数
 */
public class PolyPara {

    public static void main(String[] args) {
        /*
            方法定义的形参类型为父类类型，实参类型允许为子类类型
            定义员工类Employee，包含姓名和月工资[private]，以及计算年工资getAnnual()
            普通员工和经理继承了员工，经理类多了奖金bonus属性和管理manage()
            普通员工多了work()，普通员工和经理类要求分别重写getAnnual()
            测试类中添加一个方法showEmpAnnual(Employee e)，实现获取任何员工对象的年工资，并在main方法中调用该方法[e.getAnnual()]
            测试类中添加一个方法testWork()，如果时普通员工，则调用work()，如果时经理，则调用manage()
         */
        Worker w1 = new Worker("张三", 5000);
        Manager m1 = new Manager("milan", 5000, 200000);
        PolyParaTest polyParaTest = new PolyParaTest();
        polyParaTest.showEmpAnnual(w1);
        polyParaTest.testWork(w1);
        polyParaTest.showEmpAnnual(m1);
        polyParaTest.testWork(m1);
    }
}

class Employee {

    public String name;

    private double salary;

    public Employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public double getAnnual() {
        return salary * 12;
    }

}

class Worker extends Employee{

    public Worker(String name, double salary) {
        super(name, salary);
    }

    public void work() {
        System.out.println("普通员工 " + getName() + " is working.");
    }

    @Override
    public double getAnnual() {
        return super.getAnnual();
    }
}

class Manager extends Employee{

    private double bonus;

    public Manager(String name, double salary, double bonus) {
        super(name, salary);
        this.bonus = bonus;
    }


    public void manage() {
        System.out.println("经理 " + getName() + " is managing.");
    }

    @Override
    public double getAnnual() {
        return super.getAnnual() + bonus;
    }

}

class PolyParaTest {

    public void showEmpAnnual(Employee e) {
        System.out.println(e.getAnnual());
    }

    public void testWork(Employee e) {
        if (e instanceof Worker) {
            ((Worker) e).work();//向下转型
        } else if (e instanceof Manager) {
            ((Manager) e).manage();//向下转型
        } else {
            System.out.println("不做处理...");
        }
    }
}